#include <esp_now.h>
#include <WiFi.h>

#define LED_PIN 2  // Use GPIO2 or any other suitable GPIO for LED

// Structure to receive
typedef struct {
  uint8_t ledState;  // 1 = ON, 0 = OFF
} Data;

Data data;

// Callback function when data is received
void OnDataRecv(const esp_now_recv_info *mac, const uint8_t *incomingData, int len) {
  memcpy(&data, incomingData, sizeof(data));
  Serial.print("Received LED State: ");
  Serial.println(data.ledState);

  // Control the LED
  digitalWrite(LED_PIN, data.ledState ? HIGH : LOW);
}

void setup() {
  Serial.begin(115200);

  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);  // Start with LED off

    // Set device as Wi-Fi Station
  WiFi.mode(WIFI_STA);
  WiFi.setSleep(false);

  Serial.println("getting mac address");
  
  String macAddress = WiFi.macAddress();
  Serial.println("MAC Address: " + macAddress);

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Register callback for receiving data
  esp_now_register_recv_cb(OnDataRecv);
}

void loop() {
  // No loop code required for basic LED ON/OFF
}
